package de.kalinasch.pa.placereminder.database;

import android.database.Cursor;
import de.kalinasch.pa.placereminder.dmo.IncidentDMO;
import de.kalinasch.pa.placereminder.dmo.IncidentListItemDMO;
import de.kalinasch.pa.placereminder.dmo.PositionDMO;

/**
 * A helper class that converts db results into java objects or the other way
 * around.
 * 
 * @author dkalinasch
 */
public class ResultConverter implements I_Incident {

	/**
	 * The method creates a IncidentListDMO from the current cursor position
	 * (row).
	 * 
	 * @param _cursor
	 *            Contains the results of the db query
	 * @return the data of the current cursor position in a java object
	 */
	public static IncidentListItemDMO createIncidtentListItemDMO(Cursor _cursor) {
		IncidentListItemDMO item = new IncidentListItemDMO();
		item.setId(_cursor.getInt(_cursor.getColumnIndex(ID)));
		item.setTitle(_cursor.getString(_cursor.getColumnIndex(TITLE)));
		item.setDescription(_cursor.getString(_cursor.getColumnIndex(DESCRIPTION)));
		return item;
	}

	/**
	 * Returns all data that was saved for an incident
	 * 
	 * @param _cursor
	 *            Contains the results of the db query
	 * @return the data of the current cursor position in a java object
	 */
	public static IncidentDMO createIncident(Cursor _cursor) {
		IncidentDMO incidentdmo = new IncidentDMO();

		incidentdmo.setId(_cursor.getInt(_cursor.getColumnIndex(ID)));
		incidentdmo.setTitle(_cursor.getString(_cursor.getColumnIndex(TITLE)));
		incidentdmo.setDescription(_cursor.getString(_cursor.getColumnIndex(DESCRIPTION)));
		incidentdmo.setStreet(_cursor.getString(_cursor.getColumnIndex(STREET)));
		incidentdmo.setTown(_cursor.getString(_cursor.getColumnIndex(TOWN)));
		incidentdmo.setRadius(_cursor.getInt(_cursor.getColumnIndex(RADIUS)));
		incidentdmo.setLongitude(_cursor.getDouble(_cursor.getColumnIndex(LONGITUDE)));
		incidentdmo.setLatitude(_cursor.getDouble(_cursor.getColumnIndex(LATITUDE)));

		return incidentdmo;
	}

	/**
	 * Returns all incident in a representation it can be used to compare the
	 * location data (gps).
	 * 
	 * @param _cursor
	 *            Contains the results of the db query
	 * @return the data of the cursor in a PositionDMO
	 */
	public static PositionDMO createPositionDMO(Cursor _cursor) {
		PositionDMO posdmo = new PositionDMO();
		posdmo.setId(_cursor.getInt(_cursor.getColumnIndex(ID)));
		posdmo.setTitle(_cursor.getString(_cursor.getColumnIndex(TITLE)));
		posdmo.setDescription(_cursor.getString(_cursor.getColumnIndex(DESCRIPTION)));
		posdmo.setLongitude(_cursor.getDouble(_cursor.getColumnIndex(LONGITUDE)));
		posdmo.setLatitude(_cursor.getDouble(_cursor.getColumnIndex(LATITUDE)));
		posdmo.setRadius(_cursor.getInt(_cursor.getColumnIndex(RADIUS)));
		return posdmo;
	}
}
